package model

import (
	"gowork/fixedProcessAudit/utils"
	"time"
)

type Order struct {
	Id int64 `xorm:"pk autoincr"`
	Type int
	CustomerId int64
	AdviserId int64
	AdviserName string
	OrderAmount float64
	OrderLineItem string
	OrderStatus int
	OrderNotes string
	MealList string
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Meal struct{
	Id int64 `xorm:"pk autoincr"`
	Type int
	MealName string
	Price int64
	Number int64
	Entity bool
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Role struct {
	id int64 `xorm:"pk autoincr"`
	Name string
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Adviser struct {
	id int64 `xorm:"pk autoincr"`
	Name string
	RoleId int64
	CreatedAt time.Time
	UpdatedAt time.Time
}

type WaitingList struct {
	Id int64 `xorm:"pk autoincr"`
	OrderId int64
	ReviewerId int64
	ReviewerName string
	Status int
	OrderStatus int
	CreatedAt time.Time
	UpdatedAt time.Time
}

type OrderActivation struct {
	Id int64 `xorm:"pk autoincr"`
	OrderId int64
	Activating int64
	State int
	CreatedAt time.Time
	UpdatedAt time.Time
}

type EmotionalCounselingAudit struct {
	Id int64 `xorm:"pk autoincr"`
	OrderId int64
	Reviewer int64
	State int
	CreatedAt time.Time
	UpdatedAt time.Time
}

func (waitingList *WaitingList)ToExamine(av *Adviser, wv *WaitingList)(map[string]interface{}, error){

	w := new(utils.Worker)

	wv.Status = 2
	x.Id(wv.Id).Update(wv)

	var ov Order
	x.Id(wv.OrderId).Get(ov)

	var newWv WaitingList
	newWv.Id = w.GetId()
	newWv.OrderId = wv.OrderId
	newWv.ReviewerId = av.id
	newWv.ReviewerName = av.Name

	if wv.OrderStatus == 1{
		//if ov.OrderAmount >= 5000{
		//	newWv.OrderStatus = 2
		//	ov.OrderStatus = 2
		//}else {
		//	newWv.OrderStatus = 3
		//	ov.OrderStatus = 3
		//}

		newWv.OrderStatus = 2
		ov.OrderStatus = 2
	}else if wv.OrderStatus == 2{

		//if ov.Type == 5{
		//	var trainingCampProductList []*Meal
		//	x.Where("entity = ?", true).Find(&trainingCampProductList)
		//	var tem bool = false
		//	for _, m := range strings.Split(ov.MealList, ","){
		//		value, err := strconv.ParseInt(m, 10, 64)
		//		if err != nil{
		//			return map[string]interface{}{"code":400, "mesage":"转换失败"}, nil
		//		}
		//		for _, tcp := range trainingCampProductList{
		//			if value == tcp.Id{
		//				tem = true
		//				break
		//			}
		//		}
		//	}
		//	if tem == true{
		//		newWv.OrderStatus = 3
		//		ov.OrderStatus = 3
		//	}else {
		//		newWv.OrderStatus = 5
		//		ov.OrderStatus = 5
		//	}
		//}
		//
		//if ov.Type == 6{
		//	var entityProductList []*Meal
		//	x.Where("entity = ?", true).Find(&entityProductList)
		//	var tem bool = false
		//	for _, m := range strings.Split(ov.MealList, ","){
		//		value, err := strconv.ParseInt(m, 10, 64)
		//		if err != nil{
		//			return map[string]interface{}{"code":400, "mesage":"转换失败"}, nil
		//		}
		//		for _, tcp := range entityProductList{
		//			if value == tcp.Id{
		//				tem = true
		//				break
		//			}
		//		}
		//	}
		//	if tem == true{
		//		newWv.OrderStatus = 5
		//		ov.OrderStatus = 5
		//	}else {
		//		newWv.OrderStatus = 9
		//		ov.OrderStatus = 9
		//	}
		//}
		//
		//if ov.Type == 7{
		//
		//	var oa = OrderActivation{
		//		Id:        w.GetId(),
		//		OrderId:   ov.Id,
		//		State:     1,
		//		CreatedAt: time.Now(),
		//		UpdatedAt: time.Now(),
		//	}
		//	x.Insert(&oa)
		//
		//	newWv.OrderStatus = 7
		//	ov.OrderStatus = 7
		//}
		//
		//if ov.Type == 9{
		//	var oList []*Order
		//	x.Where("customer_id = ?", ov.CustomerId).And("type = ?", 9).Find(&oList)
		//	if len(oList) > 1 {
		//		newWv.OrderStatus = 8
		//		ov.OrderStatus = 8
		//	}else {
		//		newWv.OrderStatus = 5
		//		ov.OrderStatus = 5
		//	}
		//}

		newWv.OrderStatus = 3
		ov.OrderStatus = 3
	}else if wv.OrderStatus == 3{

		//if ov.Type == 8 || ov.Type == 10{
		//
		//	var oa = OrderActivation{
		//		Id:        w.GetId(),
		//		OrderId:   ov.Id,
		//		State:     1,
		//		CreatedAt: time.Now(),
		//		UpdatedAt: time.Now(),
		//	}
		//	x.Insert(&oa)
		//
		//	newWv.OrderStatus = 7
		//	ov.OrderStatus = 7
		//}

		newWv.OrderStatus = 4
		ov.OrderStatus = 4
	}else if wv.OrderStatus == 4{
		newWv.OrderStatus = 5
		ov.OrderStatus = 5
	}else if wv.OrderStatus == 5{
		newWv.OrderStatus = 6
		ov.OrderStatus = 6
	}else if wv.OrderStatus == 6{
		newWv.OrderStatus = 7
		ov.OrderStatus = 7
	}else if wv.OrderStatus == 7{
		newWv.OrderStatus = 8
		ov.OrderStatus = 8
	}else if wv.OrderStatus == 8{
		newWv.OrderStatus = 9
		ov.OrderStatus = 9
	}

	_, err := x.Insert(&newWv)
	if err != nil{
		return map[string]interface{}{"message": "失败","code":500}, nil
	}

	_, uErr := x.Id(wv.OrderId).Update(ov)
	if uErr != nil{
		return map[string]interface{}{"message": "失败","code":500}, nil
	}

	return map[string]interface{}{"message": "成功","code":200}, nil
}

func (waitingList *WaitingList)QueryPendingDocuments(av *Adviser)(*WaitingList, error){
	var wv WaitingList

	if av.RoleId == 0{
		x.Where("order_status = ?", 9).Find(&wv)
	}else if av.RoleId == 1{
		x.Where("order_status = ?", 8).Find(&wv)
	}else if av.RoleId == 2{
		x.Where("order_status = ?", 7).Find(&wv)
	}else if av.RoleId == 3{
		x.Where("order_status = ?", 6).Find(&wv)
	}else if av.RoleId == 4{
		x.Where("order_status = ?", 5).Find(&wv)
	}else if av.RoleId == 5{
		x.Where("order_status = ?", 4).Find(&wv)
	}else if av.RoleId == 6{
		x.Where("order_status = ?", 3).Find(&wv)
	}else if av.RoleId == 7{
		x.Where("order_status = ?", 2).Find(&wv)
	}else if av.RoleId == 8{
		x.Where("order_status = ?", 1).Find(&wv)
	}

	return &wv, nil
}



